<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input v-model="dataForm.licensePlate" placeholder="车牌号"></el-input>
      </el-form-item>
      <el-form-item label="能源类型" prop="energyType">
        <el-select style="width: 100%" v-model="dataForm.energyType" placeholder="能源类型">
          <el-option
            v-for="item in vehicelTypeList"
            :key="item.value"
            :label="item.label"
            :value="+item.value">
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="年检日期" prop="expireDate">
        <el-date-picker style="width: 100%"
                        type="date"
                        v-model="dataForm.expireDate"
                        value-format="yyyy-MM-dd"
                        placeholder="年检日期">
        </el-date-picker>

      </el-form-item>
      <el-form-item label="车辆类型" prop="type">
        <el-select style="width: 100%" v-model="dataForm.type" placeholder="车辆类型">
          <el-option
            v-for="item in trcukType"
            :key="item.value"
            :label="item.label"
            :value="+item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车辆所属" prop="belong">
        <el-select style="width: 100%" v-model="dataForm.belong" placeholder="车辆所属">
          <el-option
            v-for="item in vehicelOwnList"
            :key="item.value"
            :label="item.label"
            :value="+item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="驾驶员" prop="defaultDriverId">
        <el-select filterable style="width: 100%"  v-model="dataForm.defaultDriverId" placeholder="驾驶员">
          <el-option
            v-for="item in driverList"
            :key="item.id"
            :label="item.name"
            :value="+item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {isVehicleNumber} from '@/utils/validate'
import Utils from '@/utils/utils'
export default {
  props: ['trcukType'],
  data() {
    var licensePlate = (rule, value, callback) => {
      if (!isVehicleNumber(value)) {
        callback(new Error('车牌号不正确'))
      } else {
        callback()
      }
    }
    return {
      driverList:[],
      vehicelOwnList:[],
      visible: false,
      trcukType: [],
      vehicelTypeList: [{label: '燃油车', value: 0}, {label: '电动车', value: 1}, {label: '混动车', value: 2}],
      dataForm: {
        id: 0,
        licensePlate: '',
        energyType: '',
        expireDate: '',
        type:'',
        belong: '',
        defaultDriverId: ''
      },
      dataRule: {
        defaultDriverId: [
          {required: false, message: '请选择默认驾驶员', trigger: 'blur'},
        ],
        licensePlate: [
          {required: true, message: '车牌号不能为空', trigger: 'blur'},
          {validator: licensePlate, trigger: 'blur'}
        ],
        energyType: [
          {required: true, message: '能源类型不能为空', trigger: 'blur'}
        ],
        expireDate: [
          {required: true, message: '年检日期不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '车辆类型(吨位)不能为空', trigger: 'change'}
        ],
        belong: [
          {required: true, message: '车辆所有权', trigger: 'change'}
        ]
      }
    }
  },
  methods: {

    init(id, energyTypeList,vehicelOwnList,driverList) {
      this.vehicelTypeList = energyTypeList
      this.vehicelOwnList = vehicelOwnList
      this.driverList = driverList;
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/tms/truck/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm = data.truck
              this.dataForm.defaultDriverId = data.truck.defaultDriverId || ''
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit:Utils.debounce(function (){
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/tms/truck/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'licensePlate': this.dataForm.licensePlate,
              'energyType': this.dataForm.energyType,
              'expireDate': this.dataForm.expireDate,
              'type': this.dataForm.type,
              'createTime': this.dataForm.createTime,
              'creator': this.dataForm.creator,
              'creatorId': this.dataForm.creatorId,
              belong:this.dataForm.belong,
              defaultDriverId:this.dataForm.defaultDriverId
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },1000)
  }
}
</script>
